% MAIN

%addpath('../webcam-classifier-toolbox');
load_settings;
setup;

prefix = [data_root 'split_data/'];
basedata = {};
% basedata.save = 'iter';
% basedata.type = 'ITER_DATA';
% basedata.file = [prefix 'iter_data2.mat'];
disp('sandbox');
% basedata.save = 'special_iter';
% basedata.type = 'SPECIAL_ITER';
% basedata.file = [prefix 'special_iter_data2.mat'];
basedata.save = 'sandbox1';
basedata.type = 'SANDBOX_RUN1';
basedata.file = [prefix 'sandbox1_data2.mat'];


newfile = [prefix 'sandbox1_new_data2.mat'];

% load baseline data
disp('Loading baseline data...');
if ~exist(basedata.file,'file')
	disp('Must grab baseline data');
	dataset = getData(basedata.type); % 'SPECIAL_ITER too!
	num_cams = size(dataset,1);
	rest = setdiff(1:num_cams, 4); % remove new camera 4
	dataset = dataset(rest,:);
	num_cams = num_cams-1;

	[test_prefs test_sets test_anns train_prefs train_sets train_anns valid_prefs valid_sets valid_anns] = splitDataTTV(dataset);
	
	% right now, collapse training and validation
	XTR = [];
	YTR = [];
	XTE = [];
	YTE = [];
	ANN_TR = [];
	ANN_TE = [];
	for i = 1:num_cams
		XTR = [XTR; double(train_sets{i});  double(valid_sets{i})];
		YTR = [YTR; double(train_prefs{i}); double(valid_prefs{i})];
		XTE = [XTE; double(test_sets{i})];
		YTE = [YTE; double(test_prefs{i})];
		ANN_TR = [ANN_TR train_anns{i} valid_anns{i}];
		ANN_TE = [ANN_TE test_anns{i}];
	end
	save(basedata.file,'XTR','YTR','XTE','YTE','ANN_TR','ANN_TE','dataset');	
else
	disp('Baseline data premade!');
	load(basedata.file);
end
% ATR, XTR, YTR
% AVA, XVA, YVA
% ATE, XTE, YTE


% keyboard

% settings
settings = {};
settings.background = 0;
settings.rks = 0;
settings.ftimes = 0;
settings.pca = 0;
settings.u = [];
settings.m = [];
	
% create model
disp('Learning baseline model...');
baseline = learn_baseline(XTR,YTR,XTE,YTE,settings);

% keyboard
	
% load new data
disp('Loading new data...');
% newfile = 'sandbox1_new_data.mat';
if ~exist(newfile, 'file')
	disp('Must grab new data');
	[new_dataset] = getData('SANDBOX_RUN1');
	[NEWYTE, NEWXTE, NEWANN_TE, NEWYTR, NEWXTR, NEWANN_TR, NEWYTV, NEWXTV, NEWANN_TV] = splitDataTTV(new_dataset(4,:));
	
	NEWXTR = [NEWXTR{1}; NEWXTV{1}];
	NEWYTR = [NEWYTR{1}; NEWYTV{1}];
	NEWXTE = NEWXTE{1};
	NEWYTE = NEWYTE{1};
	NEWANN_TR = [NEWANN_TR{1} NEWANN_TV{1}];
	NEWANN_TE = NEWANN_TE{1};
	
	save(newfile, 'NEWXTR', 'NEWYTR', 'NEWXTE', 'NEWYTE', 'NEWANN_TR', 'NEWANN_TE');
else
	disp('New data premade!');
	load(newfile);
end

% evaluate
disp('Evaluating baseline model...');
new = eval_baseline(baseline, NEWXTR, NEWYTR, NEWXTE, NEWYTE, settings);

save(['model_results2/' basedata.save '_baseline_sandbox1_4_new_back_' num2str(settings.background) '_rks_' num2str(settings.rks) '_ftimes_' num2str(settings.ftimes) '_pca_' num2str(settings.pca) '.mat'], ...
	 'baseline', 'new', 'settings');
